package com.sakura.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sakura.dto.EchartsThreeDTO;
import com.sakura.entity.CheckRecord;
import com.sakura.entity.Elderly;
import com.sakura.entity.Manager;
import com.sakura.service.CheckRecordService;
import com.sakura.service.EchartsService;
import com.sakura.service.ElderlyService;
import com.sakura.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : Sakura
 * @Date : 2024/1/9 15:46
 * @Description : TODO
 */
@Service
public class EchartsServiceImpl implements EchartsService {
    @Autowired
    private CheckRecordService checkRecordService;

    @Autowired
    private ManagerService managerService;

    @Autowired
    private ElderlyService elderlyService;


    @Override
    public Map<String, List> getTwoData() {
//        首先获取近七天的日期
        List<String> recentSevenDays = getRecentSevenDays();
        List<Long> numList = new ArrayList<>();
        Map<String, List> result = new HashMap<>();
        result.put("xAxis", recentSevenDays);

        LocalDate currentDate = LocalDate.now();
        for (int i = 6; i >= 0; i--) {
            LocalDate localDate = currentDate.minusDays(i);
//            localDate
            LocalDateTime startOfDay = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endOfDay = LocalDateTime.of(localDate, LocalTime.MAX);
            LambdaQueryWrapper<CheckRecord> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.between(CheckRecord::getCreateTime, startOfDay, endOfDay);
            long count = checkRecordService.count(queryWrapper);
            numList.add(count);
        }
        result.put("yAxis", numList);
        return result;
    }


    /**
     * 获取近七天日期
     * @param :
     * @return List<String>
     * @author Sakura
     * @date 2024/1/9 15:56
     */
    public static List<String> getRecentSevenDays() {
        List<String> sevenDays = new ArrayList<>();
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");

        for (int i = 6; i >= 0; i--) {
            sevenDays.add(currentDate.minusDays(i).format(formatter));
        }

        return sevenDays;
    }

    @Override
    public List<EchartsThreeDTO> getThreeData() {
        List<Manager> list = managerService.list();
        List<EchartsThreeDTO> echartsThreeDTOList = list.stream().map(item -> {
            EchartsThreeDTO echartsThreeDTO = EchartsThreeDTO.builder()
                    .id(item.getId())
                    .name(item.getName())
                    .build();
            LambdaQueryWrapper<Elderly> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Elderly::getGridId, item.getId());
            echartsThreeDTO.setValue(elderlyService.count(queryWrapper));
            return echartsThreeDTO;
        }).collect(Collectors.toList());
        return echartsThreeDTOList;
    }
}
